<?php


namespace IDock\Insurance\JTIC;

use Exception;
use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\JTIC\Request\ConfigInquiryRequest;
use IDock\Insurance\JTIC\Request\EPolicyDownloadRequest;
use IDock\Insurance\JTIC\Request\LNewVehicleRegistrationRequest;
use IDock\Insurance\JTIC\Request\NonCarProductListRequest;
use IDock\Insurance\JTIC\Request\PolicyPaymentRequest;
use IDock\Insurance\JTIC\Request\PremiumInquiryRequest;
use IDock\Insurance\JTIC\Request\ProposalRecallRequest;
use IDock\Insurance\JTIC\Request\SpecialClausesRequest;
use IDock\Insurance\JTIC\Request\UnderwritingRequest;
use IDock\Insurance\JTIC\Request\UploadFileRequest;
use IDock\Insurance\JTIC\Request\VehicleActualValueRequest;
use IDock\Insurance\JTIC\Request\VehicleModelRequest;
use IDock\Insurance\JTIC\Response\EPolicyDownloadResponse;
use IDock\Insurance\JTIC\Response\NonCarProductListResponse;
use IDock\Insurance\JTIC\Response\PolicyPaymentResponse;
use IDock\Insurance\JTIC\Response\PremiumInquiryResponse;
use IDock\Insurance\JTIC\Response\ProposalRecallResponse;
use IDock\Insurance\JTIC\Response\SpecialClausesResponse;
use IDock\Insurance\JTIC\Response\UnderwritingResponse;
use IDock\Insurance\JTIC\Response\UploadFileResponse;
use IDock\Insurance\JTIC\Response\ValidateCarResponse;
use IDock\Insurance\JTIC\Response\VehicleActualValueResponse;
use IDock\Insurance\JTIC\Response\VehicleModelResponse;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

class JTICConstruct extends Builder implements IDockInterface
{
    public $config = [];

    public function __construct($config)
    {
        $this->config = $config;
        $this->appId = $config['appId'] ?? '';
        $this->appKey = $config['appKey'] ?? '';
    }

    public function configInquiry(): VehicleModelResponse
    {
        $request = new ConfigInquiryRequest();

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return void
     * @throws Exception
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor)
    {
        throw new Exception('续保查询---未实现的接口');
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return VehicleActualValueResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): VehicleActualValueResponse
    {
        $request = new VehicleActualValueRequest();
        $request->setVisitor($vehicleActualValueVisitor);

        /*** @var $response VehicleActualValueResponse */
        $response = $this->execute($request);
        //$response = new VehicleActualValueResponse();
        //$response->setCode(200);
        //$response->printerInit();
        return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return mixed|void
     * @throws Exception
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        throw new Exception('询价前置---未实现的接口');
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return Response
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        $request = new PremiumInquiryRequest;
        $request->setVisitor($premiumInquiryVisitor);

        //车险询价
        /*** @var PremiumInquiryResponse $mainResponse */
        $mainResponse = $this->execute($request);

        if (!$mainResponse->isSuccess() or !$mainResponse->printer->success) {
            return $mainResponse;
        }

        $mainResponse->buildCoverage($premiumInquiryVisitor->getVehicleVisitor()->isNewEnergy, $premiumInquiryVisitor->getCoverageVisitorList());

        if (!$mainResponse->isSuccess()) {
            return $mainResponse;
        }
        //非车格式化
        $mainResponse->printer->nonCarInquiryPrinter->loadVisitor($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList());

        //特约格式化
        $premiumInquiryVisitor->getExtInfoVisitor()->argc['QtnId'] = $mainResponse->printer->argc['QtnId'] ?? '';
        $specialClausesResponse = $this->specialClauses($premiumInquiryVisitor);

        if (!$specialClausesResponse->isSuccess()) {
            return $specialClausesResponse;
        }
        //加载特约
        $mainResponse->loadSpecialClauses($specialClausesResponse);

        return $mainResponse;
    }

    /**
     * 特约获取
     * @param $premiumInquiryVisitor
     * @return SpecialClausesResponse
     */
    public function specialClauses($premiumInquiryVisitor): SpecialClausesResponse
    {
        $request = new SpecialClausesRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var SpecialClausesResponse $specialClausesResponse */
        $specialClausesResponse = $this->execute($request);
        return $specialClausesResponse;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return Response
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);
//global $testResponse;
//$testResponse = '';
        /*** @var UnderwritingResponse $mainResponse */
        $mainResponse = $this->execute($request);

        if (!$mainResponse->isSuccess()) {
            return $mainResponse;
        }

        if ($mainResponse->printer->inquiryNo && !empty($premiumInquiryVisitor->getFileList())) {
            //上传文件
            $uploadRequest = new UploadFileRequest();
            $upVisitor = new UploadFileVisitor();
            $upVisitor->fileList = $premiumInquiryVisitor->getFileList();
            $upVisitor->argc = $premiumInquiryVisitor->getExtInfoVisitor()->argc;
            $upVisitor->inquiryNo = $mainResponse->printer->inquiryNo;
            $uploadRequest->setVisitor($upVisitor);
            /*** @var UploadFileResponse $response */
            $this->execute($uploadRequest);
        }

        return $mainResponse;
    }

    /**
     * 核保撤回
     * @param ProposalRecallVisitor $revokeVisitor
     * @return ProposalRecallResponse
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): ProposalRecallResponse
    {
        $request = new ProposalRecallRequest();
        $request->setVisitor($revokeVisitor);

        /*** @var ProposalRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 人工核保撤回
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return mixed|void
     * @throws Exception
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor)
    {
        throw new Exception('人工核保撤回---未实现的接口');
    }


    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return UploadFileResponse
     */
    public function uploadFile(UploadFileVisitor $uploadFileVisitor): UploadFileResponse
    {
        return new UploadFileResponse(200);
    }


    /**
     * 支付
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): PolicyPaymentResponse
    {
        $request = new PolicyPaymentRequest();
        $request->setVisitor($paymentVisitor);

        /*** @var PolicyPaymentResponse $response */
        $response = $this->execute($request);
        return $response;
    }


    /**
     * 投保单状态查询
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return void
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor)
    {
        //$request = new ProposalStatusRequest();
        //$request->setVisitor($proposalStatusVisitor);
        //
        ///*** @var ProposalStatusResponse $response */
        //$response = $this->execute($request);
        //return $response;
    }


    /**
     * 电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return EPolicyDownloadResponse
     */
    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): EPolicyDownloadResponse
    {
        $request = new EPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var EPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }


    /**
     * 保单状态查询
     * @param PolicyInfoVisitor $policyInfoVisitor
     * @return void
     */
    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor)
    {
        //$request = new PolicyInfoRequest();
        //$request->setVisitor($policyInfoVisitor);
        //
        //$response = $this->execute($request);
        //return $response;
    }

    /**
     * 非车产品
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return NonCarProductListResponse
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): Response
    {
        $request = new NonCarProductListRequest();
        $request->setVisitor($nonCarProductListVisitor);
        /*** @var NonCarProductListResponse $response */
        $response = $this->execute($request);

        $response->setSeats($nonCarProductListVisitor->seats);
        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor)
    {
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter)
    {
    }

    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        $response = new ValidateCarResponse(200);
        $response->printerInit();
        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor) :Response
    {

        $request = new LNewVehicleRegistrationRequest();

        $request->setVisitor($premiumInquiryVisitor);

        $response = $this->execute($request);

        return $response;
    }

}
